//
//  DetailOfNumViewController.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/19.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "DetailOfNumViewController.h"
#import "LuBDefine.h"
#import "DetailOfNumTableViewCell.h"
#import "LBAFNetworking.h"
#import "DetailOfNum_list.h"
#import "NoneView.h"
#import "DetailOfUserViewController.h"
#import <MJRefresh.h>
#import "DetailOfUserView.h"

@interface DetailOfNumViewController ()<UITableViewDelegate, UITableViewDataSource,DetailOfNumTableViewCellDelegate>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)NSMutableArray *detailOfNumArray;

@property (nonatomic, retain)DetailOfNum_list *detailOfNum_list;

@property (nonatomic, retain)NSMutableArray *arrayData;

@property (nonatomic, assign)NSInteger size;

@property (nonatomic, assign)BOOL isUpLoading;

@property (nonatomic, assign)CGFloat labelheight;

@property (nonatomic, assign)CGFloat  picHieght;

@end

@implementation DetailOfNumViewController

- (void)dealloc{
    [_tableView release];
    [_pid release];
    [_detailOfNum_list release];
    [_detailOfNumArray release];
    [_arrayData release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailOfNumArray = [NSMutableArray array];
        self.arrayData = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsDefaultPrompt];
    [self createDetailOfNumViewController];
    // Do any additional setup after loading the view.
}

- (void)downloadData:(NSInteger)size{
    NSString *url = [NSString stringWithFormat:@"http://api.epetbar.com/gutouv2/pet.html?do=photolinenew&month=0&limit=10&year=0&type=1&page=%ld&timeline=1&pid=%@&develop=0",self.size,self.pid];
    [LBNetWork handleGETWithUrlString:url parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
      
        NSArray *tempArray = [[responseObject objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dic in tempArray) {
         DetailOfNum_list *detailOfNum_list = [DetailOfNum_list detailOfNum_listWithDictionary:dic];
            [self.detailOfNumArray addObject:detailOfNum_list];
        }
        if (self.detailOfNumArray.count == 0) {
            NoneView *noneView = [[NoneView alloc]initWithFrame:CGRectMake(10, 0, CWIDTH, CHEIGHT)];
            [self.view addSubview:noneView];
            [noneView release];
        }else{
            UILabel *lineLabel = [[UILabel alloc]init];
            lineLabel.backgroundColor = [UIColor orangeColor];
            lineLabel.frame = CGRectMake(10, 64, 3, CHEIGHT);
            [self.view addSubview:lineLabel];
            [lineLabel release];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        if (tempArray.count == 0) {
            [self.tableView.footer endRefreshingWithNoMoreData];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

- (void)createDetailOfNumViewController{
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CWIDTH, CHEIGHT) style:UITableViewStylePlain];
    self.tableView.separatorStyle = 0;
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    [visualEffectView setFrame:CGRectMake(0, 0, CWIDTH, 64)];
    [self.view addSubview:visualEffectView];
    [visualEffectView release];
   
    [self addHeaderRefresh];
    [self addFooterUploading];
}

- (void) addHeaderRefresh{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.size = 0;
        self.isUpLoading = NO;
        [self downloadData:self.size];
    }];
    [self.tableView.header beginRefreshing];
}

- (void) addFooterUploading{
    [self.detailOfNumArray removeAllObjects];
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        self.size += 1 + 1;
        self.isUpLoading = YES;
        [self downloadData:self.size];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    NSLog(@"分区:%ld",self.detailOfNumArray.count);
    return  _detailOfNumArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"cell个数:%ld",[self.detailOfNumArray[section] detailOfNum_listArray].count);
    return [_detailOfNumArray[section] detailOfNum_listArray].count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, CWIDTH - 20, 30)];
    
    _sectionHeaderOflabelWidth = [[DetailOfNumViewController class] heightOfLabel:[(DetailOfNum_list *)self.detailOfNumArray[section] day] andTextFont:[UIFont systemFontOfSize:15] andLabelHeight:30];
     
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 10, _sectionHeaderOflabelWidth + 30, 30)];
    label.backgroundColor = [UIColor colorWithRed:0.9182 green:0.4967 blue:0.0406 alpha:1.0];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:15];
    label.layer.masksToBounds = YES;
    label.layer.cornerRadius = 15;
    label.text = [(DetailOfNum_list *)self.detailOfNumArray[section] day];
    [headerView addSubview:label];
    [self.view addSubview:headerView];
    [label release];
    
    UIImageView *pointView = [[UIImageView alloc]initWithFrame:CGRectMake(2, 0, 20, 20)];
    pointView.image = [UIImage imageNamed:@"LB_point"];
    [headerView addSubview:pointView];
    [pointView release];
    
    UIImageView *leftView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 7, 30, 30)];
    leftView.image = [UIImage imageNamed: @"LB_left_speak"];
    [headerView addSubview:leftView];
    [leftView release];
    
    return [headerView autorelease];
}

+ (CGFloat)heightOfLabel:(NSString *)text andTextFont:(UIFont *)font andLabelHeight:(CGFloat)height{
    
    CGSize size = CGSizeMake(MAXFLOAT, 30);
    NSDictionary *attributeDic = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributeDic context:nil];
    return frame.size.width;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailOfNumTableViewCell *detailOfNumCell = [DetailOfNumTableViewCell detailOfNumTableViewCellWithTableView:tableView];
    detailOfNumCell.layer.masksToBounds = YES;
    detailOfNumCell.layer.cornerRadius = 10;
    detailOfNumCell.list = [self.detailOfNumArray[indexPath.section] detailOfNum_listArray][indexPath.row];
    detailOfNumCell.delegate = self;

    return detailOfNumCell;
}

- (CGFloat)returnPhoto_desLabelOfHeight:(CGFloat)height{
    self.labelheight = height;
    return self.labelheight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    List *list = [self.detailOfNumArray[indexPath.section] detailOfNum_listArray][indexPath.row];
    CGFloat height = [list.picHeight floatValue];
    CGFloat width = [list.picWidth floatValue];
    
    CGFloat num = height / width * (CWIDTH - 40);
    return self.labelheight + num + 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
